package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.PacIndustryEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebPlatformareaEntity;
import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.service.PacIndustryService;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformService;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformareaService;
import com.baitengsoft.patentapimonitor.common.utils.PageResult;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import com.baitengsoft.patentapimonitor.common.utils.SearchParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by QEK on 2017/12/14.
 */

@RestController
@RequestMapping("/api/v1/web/typemanage")
@Api(value = "前台-系统管理-分类管理",description = "分类管理")
public class SysTypeController extends AbstractController{

    @Autowired
    private WebPlatformareaService platformService;
    @Autowired
    private PacIndustryService pacIndustryService;
    @Autowired
    private WebPlatformService webPlatformService;

    @ApiOperation(value = "获取平台的下属区域列表")
    @RequestMapping(value = "/pagequeryareas",method = RequestMethod.GET)
    public Res selectAreaByPlatformId(@RequestParam()Integer pageNum , @RequestParam() Integer pageSize){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            SearchParam searchParam = new SearchParam();
            searchParam.setPageNumber(pageNum);
            searchParam.setPageSize(pageSize);
            PageResult<WebPlatformareaEntity> list = platformService.pageSelectAreas(userInfo.getPlatformid(),searchParam);
            return Res.ok().put("list",list);
        }
        return Res.error("过期，请重新登录！");
    }

    @ApiOperation(value = "删除区域")
    @RequestMapping(value = "/deletearea/{id}",method = RequestMethod.POST)
    public Res deleteAreaById(@PathVariable(value = "id") Integer id){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            if(id > 0){
                int result = platformService.deleteAreaById(id);
                if(result > 0){
                    return Res.ok("删除成功！");
                }
                return Res.error("删除失败！");
            }
            return Res.error("请输入正确的参数！");
        }
        return Res.error("过期，请重新登录！");
    }

    @ApiOperation(value = "根据id查询区域")
    @RequestMapping(value = "/selectoneareabyid/{id}",method = RequestMethod.GET)
    public Res selectOneAreaById(@PathVariable(value = "id")Integer id){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            WebPlatformareaEntity entity = platformService.selectAreaById(id);
            return Res.ok().put("area", entity);
        }
        return Res.error("过期，请重新登录！");
    }


    @ApiOperation(value = "修改区域")
    @RequestMapping(value = "/updatearea",method = RequestMethod.POST)
    public Res updateAreaById(@RequestBody WebPlatformareaEntity entity){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo == null || userInfo.getPlatformid()==null){
            return Res.error("用户登录信息失效");
        }
        if (entity == null || entity.getSort() == null || entity.getId() ==null || StringUtils.isBlank(entity.getName()))
            return Res.error("参数有误");
        entity.setUpdateoperid(getWebUserId());
        return platformService.updateArea(entity);
    }

    @ApiOperation(value = "添加区域")
    @RequestMapping(value = "/addarea",method = RequestMethod.POST)
    public Res addArea(@RequestBody WebPlatformareaEntity entity){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            if (entity != null) {
                boolean b = platformService.checkSortIsExitByPlatformId(userInfo.getPlatformid(),entity.getSort());
                if (!b) return Res.error("区域序号已存在或者有误");
                entity.setPlatformid(userInfo.getPlatformid());
                entity.setName(entity.getName() == null || entity.getName().isEmpty() ? "" : entity.getName().trim());
                entity.setCreatedate(new Date());
                entity.setUpdatedate(new Date());
                entity.setCreateoperid(userInfo.getId());
                entity.setUpdateoperid(userInfo.getId());
                return platformService.addAreaByPlatformId(entity);
            }
        }
        return Res.error("过期，请重新登录！");
    }


    @ApiOperation("获取平台的行业列表")
    @RequestMapping(value = "/pagequerypacindustry",method = RequestMethod.GET)
    public Res pageQueryPacIndustry(@RequestParam()Integer pageNum , @RequestParam() Integer pageSize){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ){
            SearchParam searchParam = new SearchParam();
            searchParam.setPageNumber(pageNum);
            searchParam.setPageSize(pageSize);
            PageResult<PacIndustryEntity> pageResult = pacIndustryService.pageSelectPacIndustry(userInfo.getPlatformid(),searchParam);
            return Res.ok().put("list",pageResult);
        }
        return Res.error("过期，请重新登录！");
    }

    @ApiOperation("添加行业")
    @RequestMapping(value = "/addindustry",method = RequestMethod.POST)
    public Res addIndustry(@RequestBody PacIndustryEntity entity){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ){
            entity.setPlatformid(userInfo.getPlatformid());
            entity.setUpdatedate(new Date());
            entity.setCreatedate(new Date());
            entity.setCreateoperid(userInfo.getId());
            entity.setUpdateoperid(userInfo.getId());
            return pacIndustryService.addPacIndustry(entity);
        }
        return Res.error("过期，请重新登录！");
    }


    @ApiOperation(value = "根据id查询行业")
    @RequestMapping(value = "/selectoneindustrybyid/{id}",method = RequestMethod.GET)
    public Res selectOneIndustryById(@PathVariable(value = "id")Integer id){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            PacIndustryEntity entity = pacIndustryService.queryPacIndustryById(id);
            return Res.ok().put("industry", entity);
        }
        return Res.error("过期，请重新登录！");
    }

    @ApiOperation("修改行业")
    @RequestMapping(value = "/updateindustry",method = RequestMethod.POST)
    public Res updateIndustry(@RequestBody PacIndustryEntity entity){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ){
            entity.setPlatformid(userInfo.getPlatformid());
            entity.setUpdateoperid(userInfo.getId());
            return pacIndustryService.updatePacIndustry(entity);
        }
        return Res.error("过期，请重新登录！");
    }

    @ApiOperation(value = "删除行业")
    @RequestMapping(value = "/deleteindustry/{id}",method = RequestMethod.POST)
    public Res deleteIndustryById(@PathVariable(value = "id")Integer id){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            if(id > 0){
                return pacIndustryService.deletePacIndustry(id);
            }
            return Res.error("请输入正确的参数！");
        }
        return Res.error("过期，请重新登录！");
    }

    /**
     * 平台类型:1省2市3区县4园区
     * @return
     */
    @ApiOperation(value = "获取该平台的类型（省、市、区、园区）")
    @RequestMapping(value = "/getplatformtype",method = RequestMethod.GET)
    public Res getPlatformInfo(){
        WebUserinfoEntity userInfo = getUserInfo();
        if(userInfo != null ) {
            WebPlatformEntity entity = webPlatformService.queryById(userInfo.getPlatformid());
            return Res.ok().put("type",entity.getType());
        }
        return Res.error("过期，请重新登录！");
    }


}
